<!--用户主页-->
<template>
	<view class="container" style="height: 100%">
		<view style="width: 96%;margin: 0px auto;">
			<view class="logo-warp">
				<image class="logo" src="/static/image/Logo.png"></image>
			</view>
			<uni-forms style="height: 400px;">
				<view class="textSize">
					实验楼
				</view>
				<uni-forms-item>
					<ep-select v-model="buildingNo" :options="buildingList" @change="bulidingChange"></ep-select>
				</uni-forms-item>
				<view style="margin-top: 12px;" class="textSize">
					实验室
				</view>
				<uni-forms-item>
					 <ep-select v-model="flevelNo" :options="flevelList" @change="labChange"></ep-select>
				</uni-forms-item>
			</uni-forms>
			<view style="display: flex;justify-content: space-around;margin-top: -230px;">
				<view @click="inventory()" class="indexItemAll">
					<view :class=" isDown == 0 ?  'indexItemDown' : 'indexItem'">
						<image style="max-width: 40px;max-height: 36px;" src="../../static/image/inventory.png" mode=""></image>
					</view>
					<view class="text">库存</view>
				</view>
				<view @click="useSafe()" class="indexItemAll">
					<view v-if="customValue > 0" :class=" isDown == 1 ?  'indexItemDown' : 'indexItem'" style="position: relative;">
						<uni-badge :max-num="99" class="uni-badge-left-margin" style="position: absolute;left: 50%;top: -8%;" :text="customValue" type="error" absolute="rightTop" size="normal">
						</uni-badge>
						<image style="max-width: 36px;max-height: 36px;position: absolute;" src="../../static/image/Vector.png" mode="">
						</image>
					</view>
					<view v-else :class=" isDown == 1 ?  'indexItemDown' : 'indexItem'" style="position: relative;">
						<image style="max-width: 32px;max-height: 36px;" src="../../static/image/police.png" mode=""></image>
					</view>
					<view class="text">使用安全</view>
				</view>
				<view @click="record()" class="indexItemAll">
					<view :class=" isDown == 2 ?  'indexItemDown' : 'indexItem'">
						<image style="max-width: 36px;max-height: 36px;" src="../../static/image/record2.png" mode=""></image>
					</view>
					<view class="text">使用记录</view>
				</view>
			</view>
			<view class="tabBar_all">
				<view class="tarBar_item">
					<image class="tabBar_icon" :src="tabOne" mode=""></image>
				</view>
				<view v-if="sysInfo.froleid == 2" @click="tabTwoClick()" class="tarBar_item">
					<image class="tabBar_icon" :src="tabTwo" mode=""></image>
				</view>
				<view @click="tabThreeClick()" class="tarBar_item">
					<image class="tabBar_icon" :src="tabThree" mode=""></image>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	var that;
	export default {
		data() {
			return {
				interval: null,
				customValue: 0, // 使用安全是否有未读数据
				isDown: null, // 是否下凹
				buildingList: [], // 楼栋列表
				buildingName: '', // 楼栋名称
				buildingNo: '', // 楼栋编号
				flevelList: [], // 实验室列表
				flevelName: '', // 实验室名称
				flevelNo:'', // 实验室编号
				sysInfo: null, // 系统变量
				tabOne: '/static/image/home.png',
				tabTwo: '/static/image/record1.png',
				tabThree: '/static/image/me1.png',
			}
		},
		onLoad() {
			that = this
			that.sysInfo = this.$sys.getSysInfo();
			// 管理员  首页 我的-使用安全 新建加班申请
			if(that.sysInfo.froleid == 1){
				that.getBuildingListManage()
			}else{
				// 普通用户
				that.getBuildingList()
			}
			console.log('that.sysInfo',that.sysInfo)
		},
		onUnload() {
			clearInterval(that.interval)
			that.interval = null
		},
		methods: {
			getCustom(){
				that.$http.get('material/useDetail/getUseDetailCount', {
					fuseuser: that.sysInfo.userId,
					froleid: that.sysInfo.froleid,
					fpointNo: that.flevelNo
				}).then(e => {
					that.customValue = e
					console.log('e111',e)
				}).catch(()=>{
					that.customValue = 0
				})
				
				that.interval = setInterval(()=>{
					if(that.flevelNo){
						that.$http.get('material/useDetail/getUseDetailCount', {
							fuseuser: that.sysInfo.userId,
							froleid: that.sysInfo.froleid,
							fpointNo: that.flevelNo
						}).then(e => {
							that.customValue = e
							// console.log('e111',e)
						}).catch(()=>{
							that.customValue = 0
						})
					}
				},5000)
			},
			// 下拉刷新
			onPullDownRefresh(){
				clearInterval(that.interval)
				that.interval = null
				// 管理员
				if(that.sysInfo.froleid == 1){
					that.getBuildingListManage()
				}else{
					// 普通用户
					that.getBuildingList()
				}
				setTimeout(function(){
					uni.stopPullDownRefresh();
				},1000)
			},
			// 获取楼栋 -- 管理员
			getBuildingListManage(){
				that.buildingList = []
				// 场地
				that.$http.get('data/area/getList', {
					flag:35,
					arg1: that.sysInfo.fsiteNo,
				}).then(e => {
					console.log('e111',e)
					that.buildingList = e.list
					if(that.sysInfo.buildingNo&&that.sysInfo.buildingName){
						that.buildingName = that.sysInfo.buildingName
						that.buildingNo = that.sysInfo.buildingNo
						that.getLevelListOnce(that.sysInfo.buildingNo)
					}else{
						that.buildingName = that.buildingList[0].label
						that.buildingNo = that.buildingList[0].value
						that.getLevelListOnce(that.buildingList[0].value)
					}
					
				}).catch(()=>{
					that.buildingList = []
				})
			},
			
			// 获取楼栋 -- 普通用户
			getBuildingList(){
				that.buildingList = []
				// 场地
				that.$http.get('data/area/getList', {
					flag:35,
					arg1: that.sysInfo.fsiteNo,
					arg2: that.sysInfo.userId,
				}).then(e => {
					console.log('e111',e)
					that.buildingList = e.list
					if(that.sysInfo.buildingNo&&that.sysInfo.buildingName){
						that.buildingName = that.sysInfo.buildingName
						that.buildingNo = that.sysInfo.buildingNo
						that.getLevelListOnce(that.sysInfo.buildingNo)
					}else{
						that.buildingName = that.buildingList[0].label
						that.buildingNo = that.buildingList[0].value
						that.getLevelListOnce(that.buildingList[0].value)
					}
				}).catch(()=>{
					that.buildingList = []
				})
			},
			// 获取实验室
			getLevelListOnce(buildingNo){
				that.flevelList = []
				// 管理员
				if(that.sysInfo.froleid == 1){
					// 地点列表
					that.$http.get('data/area/getList', {
						flag:36,
						arg1: buildingNo,
					}).then(e => {
						console.log('e222',e)
						that.flevelList = e.list
						if(that.sysInfo.labNo&&that.sysInfo.labName){
							that.flevelName = that.sysInfo.labName
							that.flevelNo = that.sysInfo.labNo
						}else{
							that.flevelName = e.list[0].label
							that.flevelNo = e.list[0].value
						}
						
						console.log('that.sysInfo',that.sysInfo)
						that.$sys.setUser({
							userId: that.sysInfo.userId,
							token: that.sysInfo.token,
							name: that.sysInfo.userName,
							fsiteNo: that.sysInfo.fsiteNo,
							fsiteName: that.sysInfo.fsiteName,
							froleid: that.sysInfo.froleid,
							isAuto: that.sysInfo.isAuto,
							userPwd: that.sysInfo.userPwd,
							labName:  that.flevelName,
							labNo: that.flevelNo,
							buildingName: that.buildingName,
							buildingNo: that.buildingNo,
							froleidName: that.sysInfo.froleidName
						});
						that.getCustom()
					}).catch(()=>{
						that.flevelList = []
					})
				}else{
					// 普通用户
					// 地点列表
					that.$http.get('data/area/getList', {
						flag:36,
						arg1: buildingNo,
						arg2: that.sysInfo.userId,
					}).then(e => {
						console.log('e222',e)
						that.flevelList = e.list
						if(that.sysInfo.labNo&&that.sysInfo.labName){
							that.flevelName = that.sysInfo.labName
							that.flevelNo = that.sysInfo.labNo
						}else{
							that.flevelName = e.list[0].label
							that.flevelNo = e.list[0].value
						}
						console.log('that.sysInfo',that.sysInfo)
						that.$sys.setUser({
							userId: that.sysInfo.userId,
							token: that.sysInfo.token,
							name: that.sysInfo.userName,
							fsiteNo: that.sysInfo.fsiteNo,
							fsiteName: that.sysInfo.fsiteName,
							froleid: that.sysInfo.froleid,
							isAuto: that.sysInfo.isAuto,
							userPwd: that.sysInfo.userPwd,
							labName:  that.flevelName,
							labNo: that.flevelNo,
							buildingName: that.buildingName,
							buildingNo: that.buildingNo,
							froleidName: that.sysInfo.froleidName
						});
						that.getCustom()
					}).catch(()=>{
						that.flevelList = []
					})
				}
			},
			// 获取实验室
			getLevelList(buildingNo){
				that.flevelList = []
				// 管理员
				if(that.sysInfo.froleid == 1){
					// 地点列表
					that.$http.get('data/area/getList', {
						flag:36,
						arg1: buildingNo,
					}).then(e => {
						console.log('e222',e)
						that.flevelList = e.list
						that.flevelName = e.list[0].label
						that.flevelNo = e.list[0].value
						console.log('that.sysInfo',that.sysInfo)
						that.$sys.setUser({
							userId: that.sysInfo.userId,
							token: that.sysInfo.token,
							name: that.sysInfo.userName,
							fsiteNo: that.sysInfo.fsiteNo,
							fsiteName: that.sysInfo.fsiteName,
							froleid: that.sysInfo.froleid,
							isAuto: that.sysInfo.isAuto,
							userPwd: that.sysInfo.userPwd,
							labName:  e.list[0].label,
							labNo: e.list[0].value,
							buildingName: that.buildingName,
							buildingNo: that.buildingNo,
							froleidName: that.sysInfo.froleidName
						});
					}).catch(()=>{
						that.flevelList = []
					})
				}else{
					// 普通用户
					// 地点列表
					that.$http.get('data/area/getList', {
						flag:36,
						arg1: buildingNo,
						arg2: that.sysInfo.userId,
					}).then(e => {
						console.log('e222',e)
						that.flevelList = e.list
						that.flevelName = e.list[0].label
						that.flevelNo = e.list[0].value
						console.log('that.sysInfo',that.sysInfo)
						that.$sys.setUser({
							userId: that.sysInfo.userId,
							token: that.sysInfo.token,
							name: that.sysInfo.userName,
							fsiteNo: that.sysInfo.fsiteNo,
							fsiteName: that.sysInfo.fsiteName,
							froleid: that.sysInfo.froleid,
							isAuto: that.sysInfo.isAuto,
							userPwd: that.sysInfo.userPwd,
							labName:  e.list[0].label,
							labNo: e.list[0].value,
							buildingName: that.buildingName,
							buildingNo: that.buildingNo,
							froleidName: that.sysInfo.froleidName
						});
					}).catch(()=>{
						that.flevelList = []
					})
				}
			},
			// 楼栋改变
			bulidingChange(e){
				console.log('楼栋改变',e)
				that.buildingNo = e
				that.buildingList.forEach(item =>{
					if(item.value == e){
						that.buildingName = item.label
					}
				})
				that.getLevelList(e)
			},
			// 实验室改变
			labChange(e){
				console.log('实验室改变',e)
				that.flevelList.forEach(item =>{
					if(item.value == e){
						that.flevelName = item.label
					}
				})
				that.flevelNo = e
				that.$sys.setUser({
					userId: that.sysInfo.userId,
					token: that.sysInfo.token,
					name: that.sysInfo.userName,
					fsiteNo: that.sysInfo.fsiteNo,
					fsiteName: that.sysInfo.fsiteName,
					froleid: that.sysInfo.froleid,
					isAuto: that.sysInfo.isAuto,
					userPwd: that.sysInfo.userPwd,
					labName:  that.flevelName,
					labNo: e,
					buildingName: that.buildingName,
					buildingNo: that.buildingNo,
					froleidName: that.sysInfo.froleidName
				});
			},
			useSafe(){
				that.isDown = 1
				setTimeout(function(){
					that.$util.pageTo('index/useSafe');
				},10)
			},
			inventory(){
				that.isDown = 0
				setTimeout(function(){
					that.$util.pageTo('index/inventory');
				},10)
			},
			record(){
				that.isDown = 2
				setTimeout(function(){
					that.$util.pageTo('order/record');
				},10)
			},
			// 实验室预约跳转
			tabTwoClick(){
				that.$util.redirectTo('scence/index');
			},
			// 我的跳转
			tabThreeClick(){
				that.$util.redirectTo('me/index');
			},
		}
	}
</script>
<style lang="scss">
	@import url("index.css");
</style>
